package com.csxy.six.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.six.entity.Announcement;
import com.csxy.six.mapper.AnnouncementMapper;
import com.csxy.six.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements AnnouncementService {
    @Autowired
    private AnnouncementMapper announcementMapper;

    // 分页查询
    @Override
    public Page<Announcement> getPageWithCondition(Page<Announcement> page, String title,
                                                   Integer categoryId, Boolean isTop,
                                                   Boolean isPublished) {
        return (Page<Announcement>) announcementMapper.selectPageWithCondition(page, title, categoryId, isTop, isPublished);
    }

    // 设置置顶
    @Override
    public boolean setTopStatus(Integer announceId, Boolean isTop) {
        return announcementMapper.updateTopStatus(announceId, isTop) > 0;
    }

    // 发布/撤回
    @Override
    public boolean setPublishStatus(Integer announceId, Boolean isPublished) {
        return announcementMapper.updatePublishStatus(announceId, isPublished) > 0;
    }

    // 获取置顶公告
    @Override
    public List<Announcement> getTopAnnouncements() {
        return announcementMapper.selectList(new QueryWrapper<Announcement>()
                .eq("is_top", true)
                .eq("is_published", true)
                .orderByDesc("publish_time")
                .last("LIMIT 5"));
    }
}
